home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / Examples / SoundSprocketTest / TS3Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-16  |  6.7 KB  |  338 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Menu.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Copyright © 1996 Apple Computer, Inc.
  6.  */
  7.  
  8. #include <assert.h>
  9.  
  10. #include <Dialogs.h>
  11. #include <Menus.h>
  12. #include <Resources.h>
  13. #include <TextUtils.h>
  14.  
  15. #include "SoundSprocket.h"
  16.  
  17. #include "TS3Main.h"
  18. #include "TS3Menu.h"
  19. #include "TS3Resource.h"
  20. #include "TS3Sound.h"
  21. #include "TS3TestAPI.h"
  22. #include "TS3TestLoLevel.h"
  23. #include "TS3TestHiLevel.h"
  24. #include "TS3Window.h"
  25.  
  26.  
  27. typedef struct Node Node;
  28. struct Node {
  29.     Node*            lo;
  30.     Node*            hi;
  31.     Str255            name;
  32. };
  33.  
  34.  
  35. static MenuHandle    gMenuSound                = NULL;
  36. static MenuHandle    gMenuInterpolation        = NULL;
  37. static short        gMenuSoundItem            = kSoundItem_Silence;
  38. static short        gMenuInterpolationItem    = kInterpolationItem_Sinusoidal;
  39.  
  40.  
  41. static void Insert(
  42.     Node**            ioRoot,
  43.     Node*            inNode);
  44.  
  45. static void Traverse(
  46.     Node*            inNode);
  47.  
  48. static void SelectAppleMenu(
  49.     short            inItem);
  50.  
  51. static void SelectFileMenu(
  52.     short            inItem);
  53.  
  54. static void SelectSoundMenu(
  55.     short            inItem);
  56.  
  57. static void SelectInterpolationMenu(
  58.     short            inItem);
  59.  
  60.  
  61. /* =============================================================================
  62.  *        Menu_Init (external)
  63.  *
  64.  *    Initializes our menus.
  65.  * ========================================================================== */
  66. void Menu_Init(
  67.     void)
  68. {
  69.     Node*            root;
  70.     Node*            node;
  71.     short            count;
  72.     short            index;
  73.     Handle            resource;
  74.     short            resID;
  75.     ResType            resType;
  76.     Str255            resName;
  77.     
  78.     SetMenuBar(GetNewMBar(kMBarID_Main));
  79.     DrawMenuBar();
  80.     
  81.     AppendResMenu(GetMenuHandle(kMenuID_Apple), 'DRVR');
  82.     
  83.     // Get the menus that we need
  84.     gMenuSound = GetMenuHandle(kMenuID_Sound);
  85.     gMenuInterpolation = GetMenuHandle(kMenuID_Interpolation);
  86.     
  87.     // Build the sound menu
  88.     root = NULL;
  89.     
  90.     SetResLoad(false);
  91.     count = Count1Resources('snd ');
  92.     for (index = 1; index <= count; index++)
  93.     {
  94.         // Grab the resource name
  95.         resource = Get1IndResource('snd ', index);
  96.         assert(ResError() == noErr);
  97.         assert(resource != NULL);
  98.         
  99.         GetResInfo(resource, &resID, &resType, resName);
  100.         
  101.         ReleaseResource(resource);
  102.         
  103.         // Insert a new node in the tree
  104.         node = (Node*) NewPtr(sizeof(Node));
  105.         assert(node != NULL);
  106.         
  107.         node->lo = NULL;
  108.         node->hi = NULL;
  109.         BlockMove(resName, node->name, resName[0]+1);
  110.         
  111.         Insert(&root, node);
  112.     }
  113.     SetResLoad(true);
  114.     
  115.     Traverse(root);
  116.     
  117.     // Check the right interpolation item
  118.     CheckItem(gMenuInterpolation, gMenuInterpolationItem, true);
  119. }
  120.  
  121.  
  122. /* =============================================================================
  123.  *        Insert (internal)
  124.  *
  125.  *    Sorts the given node into the tree.
  126.  * ========================================================================== */
  127. void Insert(
  128.     Node**            ioRoot,
  129.     Node*            inNode)
  130. {
  131.     if (*ioRoot != NULL)
  132.     {
  133.         if (CompareString((*ioRoot)->name, inNode->name, NULL) > 0)
  134.         {
  135.             Insert(&(*ioRoot)->lo, inNode);
  136.         }
  137.         else
  138.         {
  139.             Insert(&(*ioRoot)->hi, inNode);
  140.         }
  141.     }
  142.     else
  143.     {
  144.         *ioRoot = inNode;
  145.     }
  146. }
  147.  
  148.  
  149. /* =============================================================================
  150.  *        Traverse (internal)
  151.  *
  152.  *    Appends the tree in alpha order onto the sound menu, and kills the tree.
  153.  * ========================================================================== */
  154. void Traverse(
  155.     Node*            inNode)
  156. {
  157.     if (inNode != NULL)
  158.     {
  159.         Traverse(inNode->lo);
  160.         
  161.         AppendMenu(gMenuSound, inNode->name);
  162.         
  163.         Traverse(inNode->hi);
  164.         
  165.         DisposePtr((Ptr) inNode);
  166.     }
  167. }
  168.  
  169.  
  170. /* =============================================================================
  171.  *        Menu_Exit (external)
  172.  *
  173.  *    Cleans up.
  174.  * ========================================================================== */
  175. void Menu_Exit(
  176.     void)
  177. {
  178. }
  179.  
  180.  
  181. /* =============================================================================
  182.  *        Menu_Select (external)
  183.  *
  184.  *    Takes action on the given menu item.
  185.  * ========================================================================== */
  186. void Menu_Select(
  187.     short            inMenuID,
  188.     short            inItem)
  189. {
  190.     switch (inMenuID)
  191.     {
  192.         case kMenuID_Apple:
  193.             SelectAppleMenu(inItem);
  194.         break;
  195.         
  196.         case kMenuID_File:
  197.             SelectFileMenu(inItem);
  198.         break;
  199.         
  200.         case kMenuID_Sound:
  201.             SelectSoundMenu(inItem);
  202.         break;
  203.         
  204.         case kMenuID_Interpolation:
  205.             SelectInterpolationMenu(inItem);
  206.         break;
  207.         
  208.         case kMenuID_LoLevelPreset:
  209.             TestLoLevel_Preset(inItem);
  210.         break;
  211.         
  212.         case kMenuID_HiLevelPreset:
  213.             TestHiLevel_Preset(inItem);
  214.         break;
  215.     }
  216.  
  217.     HiliteMenu(0);
  218. }
  219.  
  220.  
  221. /* =============================================================================
  222.  *        SelectAppleMenu (internal)
  223.  *
  224.  *    Takes action on the given Apple menu item.
  225.  * ========================================================================== */
  226. void SelectAppleMenu(
  227.     short            inItem)
  228. {
  229.     switch (inItem)
  230.     {
  231.         case kAppleItem_About:
  232.             Alert(kAlrtID_About, NULL);
  233.         break;
  234.     }
  235. }
  236.  
  237.  
  238. /* =============================================================================
  239.  *        SelectFileMenu (internal)
  240.  *
  241.  *    Takes action on the given File menu item.
  242.  * ========================================================================== */
  243. void SelectFileMenu(
  244.     short            inItem)
  245. {
  246.     switch (inItem)
  247.     {
  248.         case kFileItem_RunQuiet:
  249.             TestAPI_Execute();
  250.         break;
  251.         
  252.         case kFileItem_Config3DSound:
  253.             Sound_Configure();
  254.         break;
  255.         
  256.         case kFileItem_Quit:
  257.             Main_LastRoundup();
  258.         break;
  259.     }
  260. }
  261.  
  262.  
  263. /* =============================================================================
  264.  *        SelectSoundMenu (internal)
  265.  *
  266.  *    Takes action on the given Sound menu item.
  267.  * ========================================================================== */
  268. void SelectSoundMenu(
  269.     short            inItem)
  270. {
  271.     Str255            name;
  272.     
  273.     CheckItem(gMenuSound, gMenuSoundItem, false);
  274.     
  275.     switch (inItem)
  276.     {
  277.         case kSoundItem_Silence:
  278.             Sound_PlaySilence();
  279.             gMenuSoundItem = kSoundItem_Silence;
  280.         break;
  281.         
  282.         default:
  283.             GetMenuItemText(GetMenuHandle(kMenuID_Sound), inItem, name);
  284.             
  285.             if (Sound_PlayResource(name))
  286.             {
  287.                 gMenuSoundItem = inItem;
  288.             }
  289.             else
  290.             {
  291.                 gMenuSoundItem = kSoundItem_Silence;
  292.             }
  293.     }
  294.     
  295.     CheckItem(gMenuSound, gMenuSoundItem, true);
  296. }
  297.  
  298.  
  299. /* =============================================================================
  300.  *        SelectInterpolationMenu (internal)
  301.  *
  302.  *    Takes action on the given Interpolation menu item.
  303.  * ========================================================================== */
  304. void SelectInterpolationMenu(
  305.     short            inItem)
  306. {
  307.     switch (inItem)
  308.     {
  309.         case kInterpolationItem_Sinusoidal:
  310.         case kInterpolationItem_Triangular:
  311.         case kInterpolationItem_Sawtooth:
  312.             if (gMenuInterpolationItem != inItem)
  313.             {
  314.                 CheckItem(gMenuInterpolation, gMenuInterpolationItem, false);
  315.                 
  316.                 gMenuInterpolationItem = inItem;
  317.                 
  318.                 CheckItem(gMenuInterpolation, gMenuInterpolationItem, true);
  319.             }
  320.         break;
  321.     }
  322.     
  323. }
  324.  
  325.  
  326. /* =============================================================================
  327.  *        Menu_GetInterpolation (external)
  328.  *
  329.  *    Returns the current interpolation mode.
  330.  * ========================================================================== */
  331. short Menu_GetInterpolation(
  332.     void)
  333. {
  334.     return gMenuInterpolationItem;
  335. }
  336.  
  337.  
  338.